import pygame
from pygame.locals import *
from inputState import InputState
from ..images import SpriteManager
from ..audio import AudioManager

"""
Source for state base classes and state manager
"""

class Manager:
    """Manage current game state(s)"""
    
    states = []
    audio = AudioManager()
    
    def __init__(self):
        states = []
        
    
    def add(self, new):
        self.states.append(new)
        self.states[len(self.states)-1].Load()
    def remove(self, r):
        self.states[r:1] = []
    def set(self, s):
        self.states.pop()
        self.states.append(s)
        self.states[len(self.states)-1].Load()
    
    def Update(self): #Update only the top state
        InputState.Update()
        self.states[len(self.states)-1].Update()
    def Input(self): #Only check the top of the stack for input (for pop up menus and what not)
        self.states[len(self.states)-1].Input(InputState)
    def Draw(self, screen): #Draw all states, bottom to top (For semi transparent menus)
        for state in self.states:
            state.Draw(screen)

class Screen:
    """Base class for all states"""
    font = None
    fontSize = 14
    
    sprites = SpriteManager()
    
    
    def Load(self): #Load resources and set up positions
        pass
    def Input(self, input): #Handle Input Events
        pass
    def Update(self): #Update logic / animations 
        self.sprites.Update()
    def Draw(self, screen): #Draw all state components
        self.sprites.Draw(screen)
    
class Map(Screen):
    """Most used state. Will be the base class for all "levels" in the game"""
    pass

class Menu(Screen):
    """Menu State for any type of menu"""
    entries = []
    index = 0
    
    def __init__(self):
        self.elements = []
        self.entries = []
        self.index = 0
        
        self.sprites = SpriteManager()
    
    def Input(self, input):
        if input.up and self.index != 0:
            self.entries[self.index].focus = False
            self.index -= 1
            self.entries[self.index].focus = True
        if input.down and self.index != len(self.entries)-1:
            self.entries[self.index].focus = False
            self.index += 1
            self.entries[self.index].focus = True
        if input.accept or input.sword:
            self.entries[self.index].select()
    def Draw(self, screen):
        self.sprites.Draw(screen)
        for entry in self.entries:
            entry.Draw(screen)

class MenuEntry:
    """Any selectable object on the menu will derive from this"""
    
    image = None
    subimage = None
    rect = None
    
    text = None

    focus = False
    
    def __init__(self, rect, select, image, subimage, text=None):
        self.image = image
        self.subimage = subimage
        self.text = text
        self.rect = rect
        self.select = select
    
    def Draw(self, screen):
        if not self.focus:
            screen.blit(self.image, self.rect)
        else:
            screen.blit(self.subimage, self.rect)
        
        if self.text != None:
            screen.blit(self.text, self.rect)

StateManager = Manager()